package com.wits.democamera;

import android.os.Environment;
import android.util.Log;

import com.wits.av.MFormat;
import com.wits.av.MPacket;
import com.wits.av.MRecord;
import com.wits.av.MRecordQuery;
import com.wits.av.MediaSink;
import com.wits.av.MediaStorage;
import com.wits.av.MediaStorageListener;
import com.wits.av.MediaType;
import com.wits.media.PsCamera;

import java.io.File;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created by zhengboyuan on 2018-08-28.
 */

public class CameraRecorder implements MediaSink, MediaStorageListener {

    private static final String TAG = "PsCamera";

    private static final String DEVICE = "1";

    private PsCamera psCamera;
    private MediaStorage mediaStorage;
    private int channel = MediaStorage.INVALID_HANDLE;
    private int recordType = 0;

    public CameraRecorder() {
        mediaStorage = new MediaStorage();
        mediaStorage.setEventCallback(this);
    }



    public static String getStorageDir() {
        String path = Environment.getExternalStorageDirectory().getPath() + "/record";
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        return path;
    }

    public static String getDbPath() {
        String storageDir = getStorageDir();
        String dbpath = storageDir + "/record.db";
        return dbpath;
    }



    public boolean open() {
        String storageDir = getStorageDir();
        String dbpath = getDbPath();
        mediaStorage.open(dbpath);

        mediaStorage.setRecordDir(storageDir, 0.8f);

        mediaStorage.setFileDuration(30 * 1);
        mediaStorage.setFileFormat(MediaStorage.FILE_FORMAT_PS);

        channel = mediaStorage.openChannel(DEVICE);

        return mediaStorage.isOpen();
    }

    public void close() {

        if (channel != MediaStorage.INVALID_HANDLE) {
            mediaStorage.closeChannel(channel);
            channel = MediaStorage.INVALID_HANDLE;
        }

        try {
            mediaStorage.close();
        } catch (Throwable throwable) {
        }
    }

    public MediaStorage getMediaStorage() {
        return mediaStorage;
    }

    public int countRecord() {
        MRecordQuery query = new MRecordQuery();
        query.device = DEVICE;
        return mediaStorage.countRecord(query);
    }

    public int findRecord(MRecord[] records) {
        if (records == null || records.length <= 0) {
            return 0;
        }

        MRecordQuery query = new MRecordQuery();
        query.device = DEVICE;
        return mediaStorage.findRecord(query, 0, records);
    }


    public boolean startRecord(PsCamera psCamera, int recordType) {

        this.psCamera = psCamera;
        this.recordType = recordType;
        psCamera.setMediaSink(this);
        return true;
    }

    public void stopRecord() {
        if (psCamera != null) {
            psCamera.setMediaSink(null);
            psCamera = null;
        }
    }

    public boolean isRecording() {
        return (psCamera != null && channel != MediaStorage.INVALID_HANDLE);
    }


    @Override
    public void onMediaFormat(MFormat fmt) {
        mediaStorage.startRecord(channel, fmt);
        mediaStorage.setRecordType(channel, recordType);

        //mediaStorage.setRecorder(channel, "manual");
    }

    @Override
    public void onMediaPacket(MPacket pkt) {


        mediaStorage.writePacket(channel, pkt);
//        if (pkt.type == MediaType.MEDIA_TYPE_VIDEO) {
//            Log.i(TAG, "pkt size " + pkt.size());
//        }

    }

    @Override
    public void onMediaEvent(int event) {

    }

    @Override
    public void onStorageEvent(int type, String device, int record, String filepath) {
        Log.i(TAG, "event " + type);
    }
}
